package com.ruxiaoxin.share.controller;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import nl.captcha.Captcha;
import nl.captcha.servlet.CaptchaServletUtil;
import nl.captcha.text.renderer.DefaultWordRenderer;
import nl.captcha.text.renderer.WordRenderer;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.ImmutableList;

/**
 * 
 * @author ruxiaoxin
 * 
 */
@Controller
@Transactional
public class CaptchaController {
	
	private static final String CAPTCHA_ATTR = "com.ruxiaoxin.share.captcha";


	private static final List<Font> FONTS = ImmutableList.of(
																new Font("Courier New", Font.ITALIC, 32),
																new Font("Arial", Font.ITALIC, 32),
																new Font("Times New Roman", Font.ITALIC, 32),
																new Font("Verdana", Font.ITALIC, 32)
													);

	private static final List<Color> COLORS = ImmutableList.of(new Color(16, 170, 221));
	private static final WordRenderer RENDERER = new DefaultWordRenderer(COLORS, FONTS);

	/**
	 * 
	 * @param session
	 * @param out
	 * @throws IOException 
	 */

	private void writeCaptcha(Captcha captcha, HttpServletResponse response, HttpSession session) throws IOException {
		response.setContentType("image/jpeg");
		CaptchaServletUtil.writeImage(response.getOutputStream(), captcha.getImage());
		storeCaptcha(captcha, session);
	}

	void storeCaptcha(Captcha captcha, HttpSession session){
		session.setAttribute(CAPTCHA_ATTR, captcha);
	}
	
	@RequestMapping(value = "/captcha", method = RequestMethod.GET)
	public void captcha(HttpSession session, HttpServletResponse response) throws IOException {
		Captcha captcha = new Captcha.Builder(128, 32).addText(RENDERER).gimp().build();
		writeCaptcha(captcha, response, session);
	}

	@RequestMapping(value = "/captcha/validate", method = RequestMethod.GET)
	@ResponseBody
	public String isCaptchaCorrect(@RequestHeader(value = "X-Requested-With", required = false) String requestedWith, @RequestParam("captcha") String captcha,
			HttpSession session) {
		try {
			checkCaptcha(captcha, session);
			return "ok";
		} catch (Exception e) {
			return "false";
		}
	}
	
	public static void checkCaptcha(String captchaString, HttpSession session) {
		if (StringUtils.isBlank(captchaString)) {
			throw new RuntimeException("Blank captcha!");
		}

		Captcha captcha = (Captcha) session.getAttribute(CAPTCHA_ATTR);
		if (null == captcha || !captcha.isCorrect(captchaString)) {
			throw new RuntimeException("Wrong captcha!");
		}
	}
}
